<?php


namespace app\index\controller;

use think\Db;
use think\db\Where;

class Demo5
{

    /**
     * 单独查询
     */

    public function find()
    {
        /**
         * Db类数为库操作的入口类
         * 功能:静态调用think\db\Query.php类中的查询 方法实现基本操作
         * table():选择数据表
         * where():设置查询条件 表达式,数组
         * 1.单个条件 使用表达式
         * 2.对于多个条件使用数组
         * find()返回符合条件的第一条记录,没有的话返回null
         */

        $rs = Db::table('eb_user')
            ->where('uid', '=', 1)
//            ->field(['uid'=>'编号','account'=>'用户名','nickname'=>'昵称'])
            ->field(['uid' => '编号', 'nickname' => '昵称', 'account' => '微信用户名', 'phone' => '手机'])
            ->find();
        dump(is_null($rs) ? '没找到' : $rs);
    }

    public function cx()
    {
        $rs = Db::table('eb_user')
            ->where('nickname', 'like', '%石%')
            ->find();

        dump(is_null($rs) ? '没找到' : $rs);
    }

    public function findAll()
    {
        $rs = Db::table('eb_user')
            ->where('')
            ->field(['uid', 'nickname', 'phone'])
            ->order('uid', 'desc')
            ->select();
        if (!$rs) {
            return '没有数据';
        } else {
            foreach ($rs as $v) {
                dump('编号: ' . $v['uid'] . '   昵称: ' . $v['nickname'] . '   手机: ' . $v['phone']);
            }
        }


    }

    public function select()
    {
        /**
         * select()返回的是一个二维数组,没有数据 返回一个空数据
         *
         */
        $rs = Db::table('eb_user')
//            ->field('uid,nickname,account')
            ->field(['uid' => '编号', 'nickname' => '昵称', 'account' => '账号'])
            ->where([
                ['uid', '>', 15],
                ['nickname', '<>', '石平']
            ])
            ->select();
        if (empty($rs)) {
            return '没有满足条件的记录';
        } else {
            foreach ($rs as $r) {
                dump($rs);
            }


        }
    }

    public function column()
    {
        $rs = Db::table('eb_user')
//            ->where([['uid','>',1],['uid','<',10]])

            ->column('nickname', 'uid');

        dump($rs);
    }


    /**
     * 单条插入
     *
     */
    public function insert()
    {
        /*
         * insert()成功返回新增的数量,失败返回false
         */
        $data = [
            'nickname' => '小石头',
            'phone' => '13888888888'

        ];
        return Db::table('eb_user')->insert($data, true);
    }

    public function insert2()
    {
//        $s = rand(100,999);
        $data = [
            'nickname' => '小石头' . rand(1, 999),
//            'phone' => "13888888{$s}",
            'phone' => "13" . rand(100000000, 999999999)
        ];
        return Db::table('eb_user')->data($data, true)->insert();
    }

    public function insert3()
    {
        $data = [
            'nickname' => '小明' . rand(100, 999),
            'phone' => '13' . rand(100000000, 999999999),
        ];
        return Db::table('eb_user')->data($data)->insertGetId($data);
    }

    /**
     * 多条插入
     */
    public function insertAll()
    {
        $data = [
            ['nickname' => '李' . rand(100, 999), 'phone' => '17' . rand(100000000, 999999999)],
            ['nickname' => '赵小' . rand(100, 999), 'phone' => '18' . rand(100000000, 999999999)],
            ['nickname' => '小明' . rand(100, 999), 'phone' => '13' . rand(100000000, 999999999)],
            ['nickname' => '小石头' . rand(1, 999), 'phone' => "15" . rand(100000000, 999999999)]

        ];

//       return Db::table('eb_user')->insertAll($data,true);
        return Db::table('eb_user')->data($data)->insertAll();
    }

    /**
     * 更新操作
     */
    public function update()
    {
        $data = ['nickname' => '啥名字' . rand(1, 100)];
        //update()必须有更新条件
        return Db::table('eb_user')
            ->where('nickname', 'like', '%大%')
            ->where('uid', '>', 2)
            ->update($data);
    }

    /**
     * 删除操作
     */
    public function del()
    {

        return Db::table('eb_user')
            ->where(['uid' => [336, 337, 338, 331, 332, 333, 341, 340, 335, 333, 332, 334]])
            ->where(['nickname' => '啥名字27'])
            ->whereOr('nickname', 'like', '%啥%')
            ->delete();

    }

    /**原生查询 **/
    public function query()
    {
        $sql = "select nickname,phone from eb_user where uid in (1,2,3,4)";
        $rs = Db::query($sql);
        dump($rs);
    }


/**原生写操作:更新,删除,添加 **/

public function execute(){
    $rs = Db::execute("UPDATE eb_user set nickname='xiaoniba' where uid in (420,419,417)");

    dump($rs);
}

}